const {
  updateIntroduction,
  friendshipAssociationDetail
} = require('../../../../api/fraternity');

const { myToast } = require('../../../../utils/util');
// pages/active/AssistanceCenter/pages/form/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    appealType: [],
    fileList: [],
    formData: {
      id: '',
      introduction: ''
    },
    rules: {
      feedbackContent: [{
        empty: true,
        msg: '反馈内容不能为空'
      }],
    }
  },
  async onSubmit(event) {
    const {
      errors = []
    } = await updateIntroduction(this.data.formData);
    if (errors.length > 0) {
      myToast('error', '权限不足');
      return;
    }

    wx.switchTab({
      url: '/pages/fraternity/index/index',
      success: function (e) { //  每次进入 tab页都刷新一下
        var page = getCurrentPages().pop();
        
        if (page == undefined || page == null) return;
        page.onRefresh();
      }
    });
  },
  onFormData(event) { //  选择修改的内容, 注意不能异步
    const {
      prop
    } = event.currentTarget.dataset;
    this.data.formData[prop] = event.detail;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.data.formData.id = options.id;
    fetchFriendshipAssociationDetail.call(this);
  },
})

//  获取联谊会详情
async function fetchFriendshipAssociationDetail() {
  const {
    errors,
    friendshipAssociation
  } = await friendshipAssociationDetail({
    id: this.data.formData.id
  });
  if (errors.length > 0) {
    return;
  }
  console.log(friendshipAssociation.introduction);
  this.data.formData.introduction = friendshipAssociation.introduction || '';

  this.setData({
    formData: JSON.parse(JSON.stringify(this.data.formData))
  });
}